package com.netty.server;


import com.netty.message.MessageDecoder;
import com.netty.message.MessageEncoder;
import com.netty.protobuf.ProtobufDecoder;
import com.netty.protobuf.ProtobufEncoder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new IdleStateHandler(5, 0, 0, TimeUnit.MINUTES));
        //传输实体类数据
        pipeline.addLast(new ProtobufDecoder());
        pipeline.addLast(new ProtobufEncoder());
        //传输字符窜数据
//        pipeline.addLast(new StringEncoder(Charset.defaultCharset()));
//        pipeline.addLast(new StringDecoder(Charset.defaultCharset()));
        //传输自定义协议数据
        //这里使用自定义分隔符
//        pipeline.addLast(new MessageEncoder());
//        pipeline.addLast(new MessageDecoder());
        //自定义Handler
        pipeline.addLast(new ServerChannelHandler());
    }
}
